package com;

import com.trusfort.redis.JedisClient;
import com.trusfort.redis.RedisConnectionPool;
import com.util.FileUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.resps.ScanResult;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Command {
	
  private static long total = 0;
  
  private static String savePath = "D:/data/redis_exp/";
  private static String host = "192.168.1.170";
  private static String port = "6382";
  private static String limit = "2";
	
	String[] redisKeys = { 
			"XDID::aimei", 
			"XDID::abmac", 
			"XDID::amac", 
			"XDID::aaid", 
			"XDID::abid", 
			"XDID::absn",
			"XDID::afpid",
			"XDID::afdid", 
			"XDID::asn", 
			"XDID::acid"
			};

	public static void logInfo(String msg) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String cur = sdf.format(new Date());

		System.out.println(msg);
		
		FileUtils.write(savePath + "/logs/info.txt", cur + " [INFO] " + msg);
	}


	public void saveInvalidField(String fileName, String msg) {
		FileUtils.write(savePath + "/" + fileName + ".txt", msg);
	}

	/**
	 * 统计每个特征key个数
	 */
	public void statFieldCount() {

		try {

			JedisClient client = new JedisClient(host,Integer.valueOf(port),null);

			logInfo("分析安卓特征值   Key 数量开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			
			for (String key : redisKeys) {
				logInfo("  分析安卓特征值  【" + key + "】 Key数量开始");
				
				total = 0;
				getScanResultCount(client, "0", key + "*");
				
				logInfo("  分析安卓特征值  【" + key + "】 Key数量结束，共" + total + "条");
			}
			
			logInfo("分析安卓特征值   Key 数量结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

		} catch (Exception e1) {
		  e1.printStackTrace();
		}
	}

	/**
	 * 统计无效字段
	 */
	public void statInvalidField() {

		try {

			JedisClient client = new JedisClient(host,Integer.valueOf(port),null);

			logInfo("分析安卓无效特征值对应设备指纹 数量开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			
			for (String key : redisKeys) {
				
				logInfo("  分析无效特征值  【" + key + "】 对应设备指纹数量开始>>");
				
				total = 0;
				getScanResult(client, "0", key + "*");
				
				logInfo("  分析无效特征值  【" + key + "】 对应设备指纹数量结束<<");
			}
			logInfo("分析安卓无效特征值对应设备指纹 数量结束");

		} catch (Exception e1) {
		  e1.printStackTrace();
		}

	}

	public static void main(String[] args) {
		 
	  
	  savePath = args[0];
	  host = args[1];
	  port = args[2];
	  limit = args[3];
	  
	  
	  Command main = new Command();

      main.statInvalidField();
	  
		   
	}

	public void getScanResult(JedisClient client, String cursor, String pattern) {
		try {

			ScanResult<String> result2 = client.scan(DBIndex.XDID_DB_INDEX, pattern, cursor, 10000);

			java.util.List<String> keys = new ArrayList();

			String fileName = pattern.replace("*","").split("::")[1];
			
			java.util.List<String> list = result2.getResult();
			if (list != null && !list.isEmpty()) {
				for (int j = 0; j < list.size(); j++) {
					keys.add(list.get(j));
				}
				scard(fileName, keys);
			}

			System.out.println("cursor==============="+result2.getCursor());

			if (result2.getCursor() != "0") {
				getScanResult(client, String.valueOf(result2.getCursor()), pattern);
			}

		} catch (Exception e) {
		  e.printStackTrace();
		}
	}

	public static void getScanResultCount(JedisClient client, String cursor, String pattern) {
		try {

			ScanResult<String> result2 = client.scan(DBIndex.XDID_DB_INDEX, pattern, cursor, 10000);

			java.util.List<String> list = result2.getResult();
			total += list.size();

			if (result2.getCursor() != "0") {
				getScanResultCount(client, String.valueOf(result2.getCursor()), pattern);
			}

		} catch (Exception e){
		  e.printStackTrace();
		}
	}

	public void scard(String fileName, java.util.List<String> list) throws Exception {

		// 管道返回的结果集

		java.util.List<Map<String, Object>> searchResult = new ArrayList<Map<String, Object>>();

		Jedis jedis = null;
		try {
			jedis = RedisConnectionPool.getInstance().getJedis(DBIndex.XDID_DB_INDEX);

			Pipeline pipeline = jedis.pipelined();

			for (String key : list) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("key", key);
				map.put("response", pipeline.scard(key));
				searchResult.add(map);
			}
			pipeline.sync();

			if (searchResult.size() > 0) {
				for (Map<String, Object> keyResult : searchResult) {

					Response<Long> resp = (Response<Long>) keyResult.get("response");

					String key = keyResult.get("key").toString();
					if (resp.get() >= Integer.valueOf(limit)) {
					  saveInvalidField(fileName, "[" + key + "]对应设备指纹数" + resp.get());
					}
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			RedisConnectionPool.getInstance().returnResource(jedis);
		}
	}
}